/*
 * @Author: lerio.cheng
 * @Date:   2017-02-16 09:31:28
 * @Last Modified by:   DELL
 * @Last Modified time: 2017-02-24 12:49:37
 */
'use strict';

app.controller('editHouseCtrl', function($scope, $http, rest, $location, ngDialog, user, $window, tipService,uploadConf) {
    $scope.params = {};
    $scope.params.housePic = [];
    if ($window.localStorage.getItem("houseId")) {
        $scope.houseIdInfo = JSON.parse($window.localStorage.getItem("houseId"));
    }

    $scope.column2 = 0;      //分栏
    $scope.maxImgNum = 1;    //最大图片数量
    $scope.imgNew= [
        {
            text:'卧室图片',
            name: 'bedRoom',
            value: [],
            picType: 0,
            hink: '至少两张'
        }, 
        {
            text:'客厅图片',
            name: 'livingRoom',
            value: [],
            picType: 1,
            hink: '至少两张'
        },
        {
            text:'卫生间图片',
            name: 'bathRoom',
            value: [],
            picType: 2,
            hink: '至少两张'
        },
        {
            text:'厨房图片',
            name: 'kitchenRoom',
            value: [],
            picType: 3,
            hink: '至少两张'
        },
        {
            text:'周边环境',
            name: 'environment',
            value: [],
            picType: 4,
            hink: '至少两张'
        },
        {
            text:'房门照片',
            name: 'door',
            value: [],
            picType: 10,
            hink: '至少两张'
        }
    ];
    uploadConf.column2 = $scope.column2;
    uploadConf.maxImgNum =  $scope.maxImgNum;
    uploadConf.imgNew = $scope.imgNew; 

    rest.post({
        methodId: '4014',
        data: {
            houseId: $scope.houseIdInfo.id
        },
        success: function(res) {
            if (res.code == 0) {
                // $scope.params = res.res.data;
                var houseDeviceData = res.res.data.houseSource;                
                $scope.houseId = $scope.params.houseId;
                $scope.bedRoom = $scope.bedRoom || [];
                $scope.livingRoom = $scope.livingRoom || [];
                $scope.bathRoom = $scope.bathRoom || [];
                $scope.kitchenRoom = $scope.kitchenRoom || [];
                $scope.environment = $scope.environment || [];

                $scope.params = res.res.data.houseSource;
                $scope.provinceId = $scope.params.provinceId;
                $scope.cityId = $scope.params.cityId;
                $scope.countyId = $scope.params.countyId;
                $scope.streetId = $scope.params.streetId;

                $scope.getProvinceName($scope.provinceId);
                $scope.getCityName($scope.provinceId,$scope.cityId);
                $scope.getAreaName($scope.cityId,$scope.countyId);
                $scope.getStreetName($scope.countyId,$scope.streetId);

                for (var i in $scope.params.housePic) {
                    if ($scope.params.housePic[i].picType == 0) {
                        $scope.imgNew[0].value.push($scope.params.housePic[i].picPath);
                    } else if ($scope.params.housePic[i].picType == 1) {
                        $scope.imgNew[1].value.push($scope.params.housePic[i].picPath);
                    } else if ($scope.params.housePic[i].picType == 2) {
                        $scope.imgNew[2].value.push($scope.params.housePic[i].picPath);
                    } else if ($scope.params.housePic[i].picType == 3) {
                        $scope.imgNew[3].value.push($scope.params.housePic[i].picPath);
                    } else if ($scope.params.housePic[i].picType == 4) {
                        $scope.imgNew[4].value.push($scope.params.housePic[i].picPath);
                    } else if ($scope.params.housePic[i].picType == 10) {
                        $scope.imgNew[5].value.push($scope.params.housePic[i].picPath);
                    }
                }

                $scope.bed = houseDeviceData.bed ? true : false;
                $scope.sofa = houseDeviceData.sofa ? true : false;
                $scope.board = houseDeviceData.board ? true : false;
                $scope.desk = houseDeviceData.desk ? true : false;
                $scope.chair = houseDeviceData.chair ? true : false;
                $scope.washMachine = houseDeviceData.washMachine ? true : false;
                $scope.refrigerator = houseDeviceData.refrigerator ? true : false;
                $scope.television = houseDeviceData.television ? true : false;
                $scope.airCondition = houseDeviceData.airCondition ? true : false;
                $scope.waterHeater = houseDeviceData.waterHeater ? true : false;
                $scope.lampblackMachine = houseDeviceData.lampblackMachine ? true : false;
                $scope.ambry = houseDeviceData.ambry ? true : false;
                $scope.wifi = houseDeviceData.wifi ? true : false;
                $scope.stove = houseDeviceData.stove ? true : false;
                $scope.microwaveOven = houseDeviceData.microwaveOven ? true : false;

                // 户型
                $scope.houseTypeValue = $scope.params.houseType;
                if ($scope.houseTypeValue + 1) {
                    $scope.houseTypeList.map(function(ele, index) {
                        if ($scope.houseTypeValue == ele.value) {
                            $scope.houseType.selected = ele;
                        }
                    })
                }
                //朝向
                $scope.orientationValue = $scope.params.orientation;
                if ($scope.orientationValue + 1) {
                    $scope.orientationList.map(function(ele, index) {
                        if ($scope.orientationValue == ele.value) {
                            $scope.orientation.selected = ele;
                        }
                    })
                }
                //装修
                $scope.renovationValue = $scope.params.renovation;
                if ($scope.renovationValue + 1) {
                    $scope.renovationList.map(function(ele, index) {
                        if ($scope.renovationValue == ele.value) {
                            $scope.renovation.selected = ele;
                        }
                    })
                }
                //applyType
                $scope.payTypeValue = $scope.params.payType;
                if ($scope.payTypeValue + 1) {
                    $scope.payTypeList.map(function(ele, index) {
                        if ($scope.payTypeValue == ele.value) {
                            $scope.payType.selected = ele;
                        }
                    })
                }
                // 押金
                $scope.depositValue = $scope.params.deposit;
                if ($scope.depositValue + 1) {
                    $scope.depositList.map(function(ele, index) {
                        if ($scope.depositValue == ele.value) {
                            $scope.deposit.selected = ele;
                        }
                    })
                }
                // 房屋设施
                $scope.houseDevice.selected = [];
                $scope.houseDeviceList.map(function(ele, index) {
                    for (var i in $scope.params) {
                        if (i == ele.selected && $scope.params[i] == 1) {
                            $scope.houseDevice.checked.push(ele);
                        }
                    }
                });
                // 出租类型
                $scope.leaseTypeValue = $scope.params.leaseType;
                if ($scope.leaseTypeValue + 1) {
                    $scope.leaseTypeList.map(function(ele, index) {
                        if ($scope.leaseTypeValue == ele.value) {
                            $scope.leaseType.selected = ele;
                        }
                    })
                }
                // 是否已出租
                $scope.isRentValue = $scope.params.isRent;
                if ($scope.isRentValue + 1) {
                    $scope.isRentList.map(function(ele, index) {
                        if ($scope.isRentValue == ele.value) {
                            $scope.isRent.selected = ele;
                        }
                    })
                }
                // 是否可以随时入住
                $scope.isAnyTimeValue = $scope.params.isAnyTime;
                if ($scope.isAnyTimeValue + 1) {
                    $scope.isAnyTimeList.map(function(ele, index) {
                        if ($scope.isAnyTimeValue == ele.value) {
                            $scope.isAnyTime.selected = ele;
                        }
                    })
                }
            }
            
            $scope.$digest();
        },
        error: function() {}
    })   
    // 户型
    $scope.houseType = {};
    $scope.houseTypeList = [{
        name: '一室一厅',
        value: 0
    }, {
        name: '一室半',
        value: 1
    }, {
        name: '二室一厅',
        value: 2
    }, {
        name: '二室半',
        value: 3
    }, {
        name: '三室一厅',
        value: 4
    }, {
        name: '四室以上',
        value: 5
    }, {
        name: '复式',
        value: 6
    }, {
        name: '一室户',
        value: 7
    }];
    // 朝向4013
    $scope.orientation = {};
    $scope.orientationList = [{
        name: '东',
        value: 0
    }, {
        name: '南',
        value: 1
    }, {
        name: '西',
        value: 2
    }, {
        name: '北',
        value: 3
    }, {
        name: '东南',
        value: 4
    }, {
        name: '西南',
        value: 5
    }, {
        name: '东北',
        value: 6
    }, {
        name: '西北',
        value: 7
    }, {
        name: '南北',
        value: 8
    }];
    // 装修情况
    $scope.renovation = {};
    $scope.renovationList = [{
        name: '毛坯',
        value: 0
    }, {
        name: '简装',
        value: 1
    }, {
        name: '中等装修',
        value: 2
    }, {
        name: '精装修',
        value: 3
    }, {
        name: '豪华装修',
        value: 4
    }];
    // 支付方式
    $scope.payType = {};
    $scope.payTypeList = [{
        name: '一月一付',
        value: 0
    }, {
        name: '二月一付',
        value: 1
    }, {
        name: '三月一付',
        value: 2
    }];

    // 押金
    $scope.deposit = {};
    $scope.depositList = [{
        name: '无押金',
        value: 0
    }, {
        name: '一月押金',
        value: 1
    }, {
        name: '两月押金',
        value: 2
    }, {
        name: '三月押金',
        value: 3
    }];
    // 出租类型
    $scope.leaseType = {};
    $scope.leaseTypeList = [{
        name: '整租',
        value: 0
    }, {
        name: '合租',
        value: 1
    }];
    // 是否可以随时入住
    $scope.isAnyTime = {};
    $scope.isAnyTimeList = [{
        name: '是',
        value: 1
    }, {
        name: '否',
        value: 0
    }];
    // 是否已出租
    $scope.isRent = {};
    $scope.isRentList = [{
            name: '是',
            value: 1
        }, {
            name: '否',
            value: 0
        }]
        // 房屋设施
    $scope.houseDevice = {};
    $scope.houseDeviceList = [{
        name: '床',
        value: 'bed',
        id: 1
    }, {
        name: '沙发',
        value: 'sofa',
        id: 2
    }, {
        name: '餐桌',
        value: 'board',
        id: 3
    }, {
        name: '书桌',
        value: 'desk',
        id: 4
    }, {
        name: '椅子',
        value: 'chair',
        id: 5
    }, {
        name: '洗衣机',
        value: 'washMachine',
        id: 6
    }, {
        name: '冰箱',
        value: 'refrigerator',
        id: 7
    }, {
        name: '电视',
        value: 'television',
        id: 8
    }, {
        name: '空调',
        value: 'airCondition',
        id: 9
    }, {
        name: '热水器',
        value: 'waterHeater',
        id: 10
    }, {
        name: '油烟机',
        value: 'lampblackMachine',
        id: 11
    }, {
        name: '橱柜',
        value: 'ambry',
        id: 12
    }, {
        name: '无线网',
        value: 'wifi',
        id: 13
    }, {
        name: '燃气灶',
        value: 'stove',
        id: 14
    }, {
        name: '微波炉',
        value: 'microwaveOven',
        id: 15
    }];

    $scope.getProvinceName = function(provinceId) {
    //获取省份列表
        var param_1055 = {};
        param_1055.parentId = 0;
        rest.post({
            methodId: '1055',
            data: param_1055,
            success: function(res) {
                if (res.code == 0) {
                    $scope.provinceList = res.res.data;
                    for(var i in $scope.provinceList){
                        if ($scope.provinceList[i].id == provinceId) {
                            $scope.provinceid = { selected: {id: $scope.provinceId, value: true, name: $scope.provinceList[i].name} };
                        }
                    }

                } else {

                }
                $scope.disable_save_btn = false;
            },
            error: function() {
                $scope.disable_save_btn = false;
            }
        });
    }

    $scope.getCityName = function(provinceId,cityId) {
        //获取省份列表
        var param_1055 = {};
        param_1055.parentId = provinceId;
        rest.post({
            methodId: '1055',
            data: param_1055,
            success: function(res) {
                if (res.code == 0) {
                    $scope.cityList = res.res.data;
                    console.log($scope.cityList)
                    for(var i in $scope.cityList){
                        if ($scope.cityList[i].id == cityId) {
                            $scope.$apply(function(){
                                $scope.cityid = { selected: {id: $scope.cityId, value: true, name: $scope.cityList[i].name} };
                            });
                        }
                    }
                } else {

                }
                $scope.disable_save_btn = false;
            },
            error: function() {
                $scope.disable_save_btn = false;
            }
        });
    }

    $scope.getAreaName = function(cityId,areaId) {
        //获取省份列表
        var param_1055 = {};
        param_1055.parentId = cityId;
        rest.post({
            methodId: '1055',
            data: param_1055,
            success: function(res) {
                if (res.code == 0) {
                    $scope.areaList = res.res.data;
                    console.log($scope.areaList)
                    for(var i in $scope.areaList){
                        if ($scope.areaList[i].id == areaId) {
                            $scope.$apply(function(){
                                $scope.areaid = { selected: {id: $scope.areaId, value: true, name: $scope.areaList[i].name} };
                            });
                        }
                    }
                } else {

                }
                $scope.disable_save_btn = false;
            },
            error: function() {
                $scope.disable_save_btn = false;
            }
        });
    }

    $scope.getStreetName = function(areaId,streetId) {
        //获取省份列表
        var param_1055 = {};
        param_1055.parentId = areaId;
        rest.post({
            methodId: '1055',
            data: param_1055,
            success: function(res) {
                if (res.code == 0) {
                    $scope.streetList = res.res.data;
                    console.log($scope.streetList)
                    for(var i in $scope.streetList){
                        if ($scope.streetList[i].id == streetId) {
                            $scope.$apply(function(){
                                $scope.streetid = { selected: {id: $scope.streetId, value: true, name: $scope.streetList[i].name} };
                            });
                        }
                    }
                } else {

                }
                $scope.disable_save_btn = false;
            },
            error: function() {
                $scope.disable_save_btn = false;
            }
        });
    }

    /************/
    // $scope.$on('bedRoomChange', function(d, data) {
    //     deletSame(0);
    //     for (var i in data.list) {
    //         $scope.params.housePic.push({ picPath: data.list[i], picType: 0, sort: 0 });
    //     }
    // });
    // 客厅照片

    // $scope.$on('livingRoomChange', function(d, data) {
    //     deletSame(1);
    //     for (var i in data.list) {
    //         $scope.params.housePic.push({ picPath: data.list[i], picType: 1, sort: 0 });
    //     }
    // });
    // 卫生间照片

    // $scope.$on('bathRoomChange', function(d, data) {
    //     deletSame(2);
    //     for (var i in data.list) {
    //         $scope.params.housePic.push({ picPath: data.list[i], picType: 2, sort: 0 });
    //     }
    // });

    // 厨房照片

    // $scope.$on('kitchenRoomChange', function(d, data) {
    //     deletSame(3);
    //     for (var i in data.list) {
    //         $scope.params.housePic.push({ picPath: data.list[i], picType: 3, sort: 0 });
    //     }
    // });

    // 外部照片

    // $scope.$on('environmentChange', function(d, data) {
    //     deletSame(4);
    //     for (var i in data.list) {
    //         $scope.params.housePic.push({ picPath: data.list[i], picType: 4, sort: 0 });
    //     }
    // });

    // $scope.$watch('bedRoom',function(newVal,oldVal){
    //   $scope.bedRoom = newVal;
    // });
    // $scope.$watch('livingRoom',function(newVal,oldVal){
    //   $scope.livingRoom = newVal;
    // });
    // $scope.$watch('bathRoom',function(newVal,oldVal){
    //   $scope.bathRoom = newVal;
    // });
    // $scope.$watch('kitchenRoom',function(newVal,oldVal){
    //   $scope.kitchenRoom = newVal;
    // });
    // $scope.$watch('environment',function(newVal,oldVal){
    //   $scope.environment = newVal;
    // });

    // $scope.isSelected = function() {
    //     console.log($scope.houseDevice.selected);
    // };

    // $scope.chooseAll = function(){
    //     if(c==true){
    //         $scope.chooseCheckBox=true;
    //         $scope.choseArr=v;
    //     }else{
    //         $scope.chooseCheckBox=false;
    //         $scope.choseArr=[""];
    //     }
    //     flag='a';
    //     console.log("反选");
    // }

    $scope.saveData = function(a) {
    $scope.houseTypeList = [{
        name: '一室一厅',
        value: 0
    }, {
        name: '一室半',
        value: 1
    }, {
        name: '二室一厅',
        value: 2
    }, {
        name: '二室半',
        value: 3
    }, {
        name: '三室一厅',
        value: 4
    }, {
        name: '四室以上',
        value: 5
    }, {
        name: '复式',
        value: 6
    }, {
        name: '一室户',
        value: 7
    }];
        if(!$scope._provinceSelected.selected||!$scope._citySelected.selected||!$scope._areaSelected.selected){
              tipService.show("body", {
                    message: "所在城市地址必须填全",
                    notice:"danger"
                }, 3);
              return;
          }
          // if(!$scope.params.houseName){
          //     tipService.show("body", {
          //           message: "房间名称不能为空",
          //           notice:"danger"
          //       }, 3);
          //     return;
          // }
          if(!$scope.houseType.selected || !$scope.orientation.selected || !$scope.params.houseSize){
              tipService.show("body", {
                    message: "户型、朝向、面积均不能不空",
                    notice:"danger"
                }, 3);
              return;
          }
          if(!$scope.renovation.selected){
              tipService.show("body", {
                    message: "装修情况不能为空",
                    notice:"danger"
                }, 3);
              return;
          }
          if (!$scope.params.currFloor || !$scope.params.floors || $scope.params.currFloor<=0 || $scope.params.floors<=0 || $scope.params.floors<$scope.params.currFloor) {
                tipService.show("body", {
                    message: "层高、总楼层数格式不正确",
                    notice: "danger"
                }, 3);
                return;
            }
          // var regMobile = /^1[3|4|5|7|8]\d{9}$/;
          // if (!$scope.params.landlordMobile || regMobile.test($scope.params.LandlordMobile)) {

          //   tipService.show("body", {
          //           message: "房东手机号码格式不正确",
          //           notice:"danger"
          //       }, 3);
          //     return;
          // }
          if(!$scope.leaseType.selected|| !$scope.params.houseRent || !$scope.isAnyTime.selected || !$scope.isRent.selected || !$scope.payType.selected || !$scope.deposit.selected){
            tipService.show("body", {
                    message: "出租信息栏信息未填全",
                    notice:"danger"
                }, 3);
              return;
          };

        if($scope.houseDevice.selected){
            for(var i in $scope.houseDevice.selected){
                $scope.params[$scope.houseDevice.selected[i].value] = 1;
            }
        }
        
        

        $scope.params.provinceId = $scope._provinceSelected.selected.id;
        $scope.params.cityId = $scope._citySelected.selected.id;
        $scope.params.countyId = $scope._areaSelected.selected.id;
        $scope.params.orientation = $scope.orientation.selected.value;
        $scope.params.renovation = $scope.renovation.selected.value;
        $scope.params.payType = $scope.payType.selected.value;
        $scope.params.deposit = $scope.deposit.selected.value;
        $scope.params.houseType = $scope.houseType.selected.value;
        $scope.params.leaseType = $scope.leaseType.selected.value;
        $scope.params.isAnyTime = $scope.isAnyTime.selected.value;
        $scope.params.isRent = $scope.isRent.selected.value;
        
        // console.log($scope.params);
        $scope.params = angular.extend({ LandlordId: user.get('landlordId') }, $scope.params);
        // console.log($scope.params);
        rest.post({
            methodId: '4013',
            data: $scope.params,
            success: function(res) {
            	if (res.code == 0) {
	                tipService.show("body", {
	                    message: "修改数据成功,跳转中......"
	                }, 3);
	                $location.path('/app/houseOwner/list');
                } else {
                    tipService.show("body", {
                        message: "房源数据修改失败!",
                        notice: "danger"
                    }, 3);
                }
            },
            error: function() {}
       });
    };

    $scope.backList = function(e){
    	$location.path('/app/houseOwner/list');
    }
})
